<template>
  <div class="goods-hot">
    <h3>{{ TYPES[type] }}</h3>
    <!-- 商品区块 -->
    <RouterLink
      to="/"
      class="goods-item"
      v-for="item in goodList"
      :key="item.id"
    >
      <img :src="item.picture" alt="" />
      <p class="name ellipsis">{{ item.name }}</p>
      <p class="desc ellipsis">{{ item.desc }}</p>
      <p class="price">&yen;{{ item.price }}</p>
    </RouterLink>
  </div>
</template>

<script>
// 1. 不管其他的先把静态的结构搭起来
// 2. 调用接口把简单的数据获取到渲染出来
// 3. 开始通过prop参数完成组件的数据不同适配
import { findHotGoods } from '@/api/good'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
// 准备一个枚举对象
const TYPES = {
  1: '24热榜',
  2: '周热榜',
  3: '总热榜'
}
export default {
  name: 'hot-index',
  // props中的数据会自动代理到当前组件的实例身上
  props: {
    type: {
      type: String
    }
  },
  setup (props) {
    const goodList = ref([])
    const route = useRoute()
    const loadList = async () => {
      const res = await findHotGoods({
        id: route.params.id,
        type: props.type // 决定当前要请求的热榜类型
      })
      goodList.value = res.data.result
    }
    loadList()
    return {
      goodList,
      TYPES
    }
  }
}
</script>

<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  .goods-item {
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
}
</style>
